/**
 * 
 */
package com.cmpanyandi.elearning.wordsdriller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.cmpanyandi.elearning.utils.db.DbAdapter;
import com.cmpanyandi.elearning.wordsdriller.model.Dictionary;
import com.cmpanyandi.elearning.wordsdriller.model.DictionaryItem;

/**
 * Dictionary Item Details activity.
 * 
 * @author wojtek
 * 
 */
public class DictionaryItemEdit extends TabActivity {

    private EditText nameText;
    private EditText contextText;
    private EditText descriptionText;
    private Dictionary dictionary;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	setContentView(R.layout.dictionary_item_edit);
	super.onCreate(savedInstanceState);
	
	nameText = (EditText) findViewById(R.id.name);
	contextText = (EditText) findViewById(R.id.context);
	descriptionText = (EditText) findViewById(R.id.description);

	dictionary = new Dictionary(getIntent().getExtras().getLong(DictionaryItem.DICTIONARY));
	populateFieldsFromIntent(getIntent());
	initTab();
	ImageView save = (ImageView) findViewById(R.id.mainSave);
	save.setOnClickListener(new View.OnClickListener() {

	    public void onClick(View view) {
		setResult(RESULT_OK);
		finish();
	    }

	});
    }
    
    
    private void initTab() {
	tabHost = getTabHost();
	 tabHost.addTab(tabHost.newTabSpec(DictionaryItem.CONTEXT).setIndicator(getText(R.string.context)).setContent(R.id.context));
	 tabHost.addTab(tabHost.newTabSpec(DictionaryItem.DESCRIPTION).setIndicator(getText(R.string.description)).setContent(R.id.description));
	 tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator(getText(R.string.repetition_status)).setContent(R.id.rephistory));
	 tabHost.setCurrentTab(0);

	
    }

    private void populateFields() {
	if (pk != null && pk.longValue() > 0) {
	    DictionaryItem item = (DictionaryItem) DbAdapter.getInstance().getByPK(new DictionaryItem(pk));

	    nameText.setText(item.getName());
	    contextText.setText(item.getContext());
	    descriptionText.setText(item.getDescription());
	    dictionary = item.getDictionary();
	}
    }
    
    private void populateFieldsFromIntent(Intent intent) {
	
	    nameText.setText(intent.getExtras().getString(DictionaryItem.NAME));
	    contextText.setText(intent.getExtras().getString(DictionaryItem.CONTEXT));
	    descriptionText.setText(intent.getExtras().getString(DictionaryItem.DESCRIPTION));
	    dictionary = new Dictionary(intent.getExtras().getLong(DictionaryItem.DICTIONARY));
	
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	outState.putLong(getStateProperty(), pk);
    }

    @Override
    protected void onPause() {
	super.onPause();
	saveState();
    }

    @Override
    protected void onResume() {
	super.onResume();
	populateFields();
    }

    private void saveState() {
	DictionaryItem item = new DictionaryItem(pk);
	item.setName(nameText.getText().toString());
	item.setDescription(descriptionText.getText().toString());
	item.setContext(contextText.getText().toString());
	item.setDictionary(dictionary);
	DbAdapter.getInstance().saveOrUpdate(item);
	pk = item.getPk();
    }

    protected String getStateProperty() {
	return "state_item";
    }

}
